﻿namespace NUnitTestProject
{
    using JanusLibAing;
    using NUnit.Framework;

    /// <summary>
    /// This is a test class for EncryptorXteaTest and is intended
    /// to contain all EncryptorXteaTest Unit Tests
    /// </summary>
   [TestFixture]
    public class EncryptorXteaTest
    {
        // key must be 16 bytes
        uint[] key = new uint[]
                             {
                                 0x78563412, 0xf0cdcb9a,
                                 0x48372615, 0xc0bfae9d
                             };

        [Test]
        public void Xtea1EncipherTest01()
        {
            // value must be 8 bytes
            uint[] value = new uint[]{0x3f180441, 0x3cc494ae};

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Encipher(EncryptorXtea.NumRounds, value, key);
            Assert.AreEqual(value[0], 0x12345678);
            Assert.AreEqual(value[1], 0x87654321);
        }

        [Test]
        public void Xtea1EncipherTest02()
        {
            // value must be 8 bytes
            uint[] value = new uint[] { 0xa6d091be, 0xa1f28320 };

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Encipher(EncryptorXtea.NumRounds, value, key);
            Assert.AreEqual(value[0], 0x00310032);
            Assert.AreEqual(value[1], 0x00330034);
        }

        [Test]
        public void Xtea1EncipherTest03()
        {
            // value must be 8 bytes
            uint[] value = new uint[] { 0x00310032, 0x00330034 };

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Encipher(EncryptorXtea.NumRounds, value, key);
            Assert.AreEqual(value[0], 0x172a46ef);
            Assert.AreEqual(value[1], 0x88e33598);
        }

        [Test]
        public void Xtea1DecipherTest03()
        {
            // value must be 8 bytes
            uint[] value = new uint[] { 0x172a46ef, 0x88e33598 };

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Decipher(EncryptorXtea.NumRounds, value, key);
            Assert.AreEqual(value[0], 0x00310032);
            Assert.AreEqual(value[1], 0x00330034);
        }

        [Test]
        public void Xtea1EncipherTest03_00()
        {
            // value must be 8 bytes
            uint[] value = new uint[] { 0x00310032, 0x00330034 };

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Encipher(0, value, key);
            Assert.AreEqual(value[0], 0x30b01251);
            Assert.AreEqual(value[1], 0x31bf6553);
        }

        [Test]
        public void Xtea1DecipherTest03_00()
        {
            // value must be 8 bytes
            uint[] value = new uint[] { 0x30b01251, 0x31bf6553 };

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Decipher(0, value, key);
            Assert.AreEqual(value[0], 0x00310032);
            Assert.AreEqual(value[1], 0x00330034);
        }

        [Test]
        public void Xtea1EncipherTest03_01()
        {
            // value must be 8 bytes
            uint[] value = new uint[] { 0x00310032, 0x00330034 };

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Encipher(1, value, key);
            Assert.AreEqual(value[0], 0x46267ef0);
            Assert.AreEqual(value[1], 0xba3e6be4);
        }

        [Test]
        public void Xtea1DecipherTest03_01()
        {
            // value must be 8 bytes
            uint[] value = new uint[] { 0x46267ef0, 0xba3e6be4 };

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Decipher(1, value, key);
            Assert.AreEqual(value[0], 0x00310032);
            Assert.AreEqual(value[1], 0x00330034);
        }


        [Test]
        public void Xtea1DecipherTest01()
        {
            // value must be 8 bytes
            uint[] value = new uint[] { 0x12345678, 0x87654321 };

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Decipher(EncryptorXtea.NumRounds, value, key);
            Assert.AreEqual(value[0], 0x3f180441);
            Assert.AreEqual(value[1], 0x3cc494ae);
        }

        [Test]
        public void Xtea1DecipherTest02()
        {
            // value must be 8 bytes
            uint[] value = new uint[] { 0x00310032, 0x00330034 };

            EncryptorXtea target = new EncryptorXtea();
            target.Xtea1Decipher(EncryptorXtea.NumRounds, value, key);
            Assert.AreEqual(value[0], 0xa6d091be);
            Assert.AreEqual(value[1], 0xa1f28320);
        }
       
       public void TestEncriptString(string input, string expected)
        {
            string output;
            EncryptorXtea target = new EncryptorXtea();
            output = target.Encrypt(input, key);
            Assert.AreEqual(output, expected, string.Format("For input {0} was not expected result", input));
        }

        public void TestDecriptString(string input, string expected)
        {
            string output;
            EncryptorXtea target = new EncryptorXtea();
            output = target.Decrypt(input, key);
            Assert.AreEqual(output, expected, string.Format("For input {0} was not expected result", input));
        }

        [Test]
        public void TestEncriptString01()
        {
            TestEncriptString("12345678", "172A46EF88E33598892E3AEE7FD0B0AA");
        }

        [Test]
        public void TestDecriptString01()
        {
            TestDecriptString("172A46EF88E33598892E3AEE7FD0B0AA", "12345678");
        }

        [Test]
        public void TestEncriptString02()
        {
            TestEncriptString("1234567", "172A46EF88E335986434ACA27240BE87");
        }

        [Test]
        public void TestDecriptString02()
        {
            TestDecriptString("172A46EF88E335986434ACA27240BE87", "1234567");
        }

        [Test]
        public void TestEncriptString03()
        {
            TestEncriptString("", "");
        }

        [Test]
        public void TestDecriptString03()
        {
            TestDecriptString("", "");
        }

        [Test]
        public void TestEncriptString04()
        {
            TestEncriptString("abcde", "4B69F2A957DA6524AC5194882661447F");
        }

       [Test]
        public void TestDecriptString04()
        {
            TestDecriptString("4B69F2A957DA6524AC5194882661447F", "abcde");
        }


       [Test]
       public void TestEncriptString05()
       {
           TestEncriptString("абвгд", "10F2294D24D81672B2D73D252934A859");
       }

       [Test]
       public void TestDecriptString05()
       {
           TestDecriptString("10F2294D24D81672B2D73D252934A859", "абвгд");
       }

       [Test]
       public void TestEncriptString06()
       {
           // 172a46ef88e33598
           TestEncriptString("1234", "172A46EF88E33598");
       }

       [Test]
       public void TestDecriptString06()
       {
           TestDecriptString("172A46EF88E33598", "1234");
       }
    }
}
